//
//  YYT_FansFocusViewController.m
//  AudioVisualFeast
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YYT_FansFocusViewController.h"
#import "YYT_fansNavigationView.h"
#import "YYT_fansTableview.h"
#import "YYT_AFNTools.h"
#import <MJRefresh.h>
#import "YYT_MVPlayerViewController.h"

@interface YYT_FansFocusViewController ()<cellClick,fansFocusDelegate>
@property (nonatomic,retain) NSMutableArray *fansArray;
@property (nonatomic,retain) YYT_fansNavigationView *fansNavc;
@property (nonatomic,retain) YYT_fansTableview *tableview;
// 网络请求参数
@property (nonatomic, assign)NSInteger num;
@property (nonatomic, retain)NSString *area;
@end

@implementation YYT_FansFocusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.fansArray = [NSMutableArray array];
    
    self.fansNavc = [[YYT_fansNavigationView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 44)];
    self.fansNavc.delegate = self;
    [self.view addSubview:self.fansNavc];
    
    self.tableview = [[YYT_fansTableview alloc]initWithFrame:CGRectMake(0, (64+44), WIDTH, HEIGHT-44-64-49) style:UITableViewStylePlain];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.fansDelegate = self;
    [self.view addSubview:self.tableview];
    
    // 网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 网络请求 area = ALL  全部
        self.area = @"ALL";
        [self getData:self.num withArea:self.area];
    });
    [self addheader];
    [self addfooter];
}

#pragma mark -- 上拉刷新
-(void)addfooter {
    self.tableview.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num += 20;
            [self getData:self.num withArea:self.area];
            [self.tableview.tableview.mj_footer endRefreshing];
        });
    }];
    
}
#pragma mark -- 下拉刷新
-(void)addheader {
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num = 0;
            [self.fansArray removeAllObjects];
            [self getData:self.num withArea:self.area];
            [self.tableview.mj_header endRefreshing];
        });
    }];
    [self.tableview.mj_header beginRefreshing];
}


// 数据请求
// http://mapi.yinyuetai.com/suggestions/fan_focus.json?D-A=0&offset=0&size=20&type=ALL

-(void)getData:(NSInteger)dataNum withArea:(NSString *)area{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *txtFilePath = [documentPath stringByAppendingPathComponent:@"/huancun.xml"];
    [YYT_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/suggestions/fan_focus.json?D-A=0" parameter:@{@"offset":[NSString stringWithFormat:@"%ld", dataNum], @"size":@"20", @"type":area} httpHeader:@{@"App-Id":@"10101026",@"Device-Id":@"587d897556663eb999e31a4c621abfbc",@"Device-V":@"aVBob25lIE9TXzkuMl82NDAqMTEzNl8xMDAwMDEwMDBfaVBob25lNiwy"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        NSArray *array = dictionary[@"videos"];
        for (NSDictionary *dic in array) {
            YYT_ModleShared *model = [[YYT_ModleShared alloc] initWithDictionary:dic];
            [self.fansArray addObject:model];
        }
        [_tableview.tableview reloadData];
        self.tableview.fansArray = self.fansArray;
        
    } fail:^(NSError *error) {
        [self.fansArray removeAllObjects];
        NSArray *array = [NSArray arrayWithContentsOfFile:txtFilePath];
        for (NSDictionary *dic in array) {
            YYT_ModleShared *model = [[YYT_ModleShared alloc] initWithDictionary:dic];
            [self.fansArray addObject:model];
        }
        [self.tableview.tableview reloadData];
    }];
}

#pragma mark -- cellClick协议
-(void)cellClick:(NSInteger)item {
    
    //   全部 ALL
    //   字幕 SUBTITLE
    //   鬼畜 BEAST
    //   强推 STRONG_PUSH
    //   饭拍 FAN_SHOT
    //   饭制 FAN_PRODUCTION
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (item == 5 || item == 11 || item == 17) {
            self.area = @"FAN_PRODUCTION";
            [self.fansArray removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 0 || item == 6 || item == 12) {
            self.area = @"ALL";
            [self.fansArray removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 1 || item == 7 || item == 13) {
            self.area = @"SUBTITLE";
            [self.fansArray removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 2 || item == 8 || item == 14) {
            self.area = @"BEAST";
            [self.fansArray removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 3 || item == 9 || item == 15) {
            self.area = @"STRONG_PUSH";
            [self.fansArray removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 4 || item == 10 || item == 16) {
            self.area = @"FAN_SHOT";
            [self.fansArray removeAllObjects];
            [self getData:self.num withArea:self.area];
        }
        // 不让滚动
        [_tableview setContentOffset:CGPointMake(0, 0)];
    });
}

- (void)pushToDetailVCtableview:(UITableView *)tableview didSelectIndexPath:(NSIndexPath *)indexPath{
    YYT_MVPlayerViewController *mvplay = [[YYT_MVPlayerViewController alloc] init];
    mvplay.playurl = [self.fansArray[indexPath.row]urls]; // 传一个每个cell 相对应的网址、
    mvplay.headtitle = [self.fansArray[indexPath.row] title];// 传一个标题
    mvplay.ids = [NSString stringWithFormat:@"%@", [self.fansArray[indexPath.row] ids]];
    mvplay.pic = [NSString stringWithFormat:@"%@",[self.fansArray[indexPath.row] playListPic]];
    [self.navigationController pushViewController:mvplay animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
